/*
 * @Description: 工具函数
 * @Author: wws1993
 * @Date: 2020-11-25 17:24:45
 * @LastEditTime: 2020-12-04 17:16:42
 * @LastEditors: wws1993
 * @FilePath: \TODO-MANAGER\src\lib\index.ts
 */

import Vue from 'vue';

export default {
  notify(title: string, message: string, type?: 'success'|'warning'|'error'|'info') {
    Vue.prototype.$notify({title, message, type, duration: 3000});
  },
};

/** Date 原型方法拓展 */
Date.prototype.format = function(fmt: string = 'yyyy/MM/dd hh:mm:ss') {
  const o: {[k: string]: any} = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds(), // 毫秒
    'w+': Math.ceil((this.getDate() + 6 - this.getDay()) / 7),
  };

  if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, String(this.getFullYear()).substr(4 - RegExp.$1.length)); }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(String(o[k]).length)); }
  }
  return fmt;
};
Date.prototype.calcDependentCode = function() {
  const date = this.getDate();
  const month = this.getMonth();
  const year = this.getFullYear();

  const currentMonthFirstDay = new Date(year, month, 1);
  const currentMonthLastDay = new Date(year, month + 1, 0);

  return {
    currentMonthFirstDay,
    currentMonthFirstDayWeek: currentMonthFirstDay.getDay(),
    currentMonthLastDay,
    currentMonthLastDayWeek: currentMonthLastDay.getDay(),
    maxDate: currentMonthLastDay.getDate(),
  };
};
Date.prototype.getNextDay = function() {
  const date = this.getTime();

  return new Date(date + 86400000);
};
Date.prototype.getNextWeek = function() {
  const date = this.getTime();

  return new Date(date + 86400000 * 7);
};
Date.prototype.getNextMonth = function() {
  return new Date(this.setMonth(this.getMonth() + 1));
};
Date.prototype.getPrevDay = function() {
  const date = this.getTime();

  return new Date(date - 86400000);
};
Date.prototype.getPrevWeek = function() {
  const date = this.getTime();

  return new Date(date - 86400000 * 7);
};
Date.prototype.getPrevMonth = function() {
  return new Date(this.setMonth(this.getMonth() - 1));
};
Date.prototype.isRange = function(start: Date, end: Date) {
  const cur = this.format('yyyy-MM-dd');
  const min = start.format('yyyy-MM-dd');
  const max = end.format('yyyy-MM-dd');

  if (cur === min && cur === max) {
    return 'close';
  } else if (cur === min) {
    return 'close-left';
  } else if (cur === max) {
    return 'close-right';
  } else {
    return 'open';
  }
};
